package com.ruoyi.dm.component;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.CollectFileTypeConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.biz.BizModule;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.dm.domain.po.AirCollectRecordPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDate;

/**
 * 天基 大气风场 uars
 */
@Component
@Slf4j
public class WindUarsDataStrategy implements DataStrategy<AirCollectRecordPO> {

    private static String TYPE_SZONWIN = "SZONWIN";
    private static String TYPE_SMERWIN = "SMERWIN";

    @Override
    public AirCollectRecordPO makeupPo(AirCollectRecordPO po) {
        //以 1991, 9, 12为起始日
        //HRDI_L3AT_SMERWIN_A_D0056.V0011_C01_PROD
        String name = po.getFileName();
        String[] str = name.split("_");
        String dataType = str[3];
        LocalDate of = LocalDate.of(1991, 9, 12);
        of = of.plusDays(Integer.valueOf(str[4].substring(1,5)));
        po.setFileDate(of);
        po.setDataType(dataType);
        return po;
    }

    @Override
    public String pythonCmd(String satellite, String dataType, String filePath, String method, Integer year) {

        // 对于大气风场 uars  SZONWIN和 SMERWIN 成对出现
        //HRDI_L3AT_SZONWIN_A_D0056.V0011_C01_PROD
        //HRDI_L3AT_SMERWIN_A_D0056.V0011_C01_PROD
        File file = new File(filePath);
        String otherFile = "";
        String name = file.getName();
        if (name.toUpperCase().contains(TYPE_SZONWIN)) {
            //判断另一类是否存在
            otherFile = filePath.replace(TYPE_SZONWIN, TYPE_SMERWIN);
            if (!FileUtil.exist(otherFile)) {
                throw new ServiceException("数据缺失:"+otherFile);
            }
        }else if (name.toUpperCase().contains(TYPE_SMERWIN)) {
            //判断另一类是否存在
            otherFile = filePath.replace(TYPE_SMERWIN, TYPE_SZONWIN);
            if (!FileUtil.exist(otherFile)) {
                throw new ServiceException("数据缺失:"+otherFile);
            }
        }else {
            throw new ServiceException("类型异常,不是 "+TYPE_SMERWIN+" 和 "+TYPE_SZONWIN);
        }
        log.info("相应文件存在:{}",otherFile);

        String fileType = CollectFileTypeConstants.TYPE_WIND;
        String outPath = PathUtil.outputModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result") ;
        String logPath = PathUtil.tmpModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("result");
        //创建文件夹 若无
        FileUtil.mkdir(outPath);
        FileUtil.mkdir(logPath);
        String[] cmdLines = new String[8];
        //python Nimbus7_analysis.py --filepath= --outfile= --outlog=
        String script = PathUtil.codeModuleDir(BizModule.COLLECT) + PathUtil.moduleDirs().getCollect().get("uarsScript");
        cmdLines = new String[8];
        cmdLines[0] = "python";
        cmdLines[1] = script;
        cmdLines[2] = "--filepath";
        cmdLines[3] = JSON.toJSONString(new String[]{filePath,otherFile});
        cmdLines[4] = "--outfile";
        cmdLines[5] = outPath;
        cmdLines[6] = "--outlog";
        cmdLines[7] = logPath;
        String commond = ArrayUtil.join(cmdLines, " ");
        log.info("拼接脚本:{}", commond);
        return commond;
    }
}
